﻿using ClassLibrary1;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        SocketFunc socket;
        System.Action<string> ReceiveAction;
        System.Action AccessAction;
        public Form1()
        {
            InitializeComponent();
            //异步建立连接回调
            AccessAction = () =>
            {
                this.Invoke((MethodInvoker)delegate()
                {

                    String friendIP = socket.CurSocket.RemoteEndPoint.ToString();


                    try
                    {
                        socket.Receive(ReceiveAction);
                    }
                    catch (Exception exp)
                    {
                        MessageBox.Show(this, exp.Message);
                    }
                });

            };
            //异步接收消息回调
            ReceiveAction = msg =>
            {
                this.Invoke((MethodInvoker)delegate()
                {
                    MessageBox.Show(this, msg);
                });
            };
            this.socket = new ServerSocket();
            this.socket.Access("", this.AccessAction);
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            socket.Send(textBox1.Text);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            socket.CurSocket.Close();
        }

    }
}
